package com.nmf.site.biz.applybill.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.nmf.dao.gen.bean.BizApplyBill;
import com.nmf.site.biz.applybill.service.BillZhuanjiaService;
import com.nmf.site.biz.applybill.service.BizApplyBillService;
import com.nmf.site.biz.applybill.vo.BillZhuanjiaVo;
import com.nmf.site.common.config.Global;
import com.nmf.site.common.excep.SiteException;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.StringUtils;
import com.nmf.site.common.web.BaseController;

@Controller
@RequestMapping(value = "${adminPath}/applybill/zhuanjia/applyBill")
public class BillZhuanjiaController extends BaseController{
	@Autowired
	BizApplyBillService bizApplyBillService;
	@ModelAttribute
	public BizApplyBill get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizApplyBill entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizApplyBillService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizApplyBill();
		}
		return entity;
	}
	
	@RequiresPermissions("applybill:zhuanjia:view")
	@RequestMapping(value = {"form"})
	public String form(BillZhuanjiaVo billZhuanjiaVo, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			BizApplyBill bizApplyBill = bizApplyBillService.get(billZhuanjiaVo.getId());
			model.addAttribute("bizApplyBill", bizApplyBill);
			
			BillZhuanjiaVo billZhuanjiaVoDb = BillZhuanjiaService.formatBillZhuanjia(bizApplyBill);
			model.addAttribute("billZhuanjiaVo", billZhuanjiaVoDb);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/applybill/zj/bizZhuanjiaBillForm";
		}
		return "biz/applybill/zj/bizZhuanjiaBillForm";
	}
	
	
	@RequiresPermissions("applybill:zhuanjia:view")
	@RequestMapping(value = {"detail"})
	public String detail(BillZhuanjiaVo billZhuanjiaVo, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			BizApplyBill bizApplyBill = bizApplyBillService.get(billZhuanjiaVo.getId());
			model.addAttribute("bizApplyBill", bizApplyBill);
			
			BillZhuanjiaVo billZhuanjiaVoDb = BillZhuanjiaService.formatBillZhuanjia(bizApplyBill);
			model.addAttribute("billZhuanjiaVo", billZhuanjiaVoDb);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/applybill/bizApplyBillForm";
		}
		return "biz/applybill/bizApplyBillForm";
	}
	
	@RequiresPermissions("applybill:zhuanjia:view")
	@RequestMapping(value = {"list"})
	public String list(BizApplyBill bizApplyBill, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizApplyBill> page = bizApplyBillService.findPage(new Page<BizApplyBill>(request, response), bizApplyBill); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/applybill/zj/bizApplyBillList";
		}
		return "biz/applybill/zj/bizApplyBillList";
	}
	@RequiresPermissions("applybill:zhuanjia:edit")
	@RequestMapping(value = "save")
	public String save(BizApplyBill bizApplyBill, Model model, RedirectAttributes redirectAttributes) {
		BizApplyBill bizApplyBillDb = bizApplyBillService.get(bizApplyBill.getId());
		BillZhuanjiaService.setZJinfos(bizApplyBill);
		try{
			bizApplyBillService.save(bizApplyBill);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/applybill/zhuanjia/applyBill/list?repage";
		}
		addMessage(redirectAttributes, "保存申请表成功");
		return "redirect:"+Global.getAdminPath()+"/applybill/zhuanjia/applyBill/list?repage";
	}
}
